/*
 * Copyright 2008-2023 dexian.vip. All rights reserved.
 * Support: http://www.dexian.vip
 * License: http://www.dexian.vip/license
 */

package vip.dexian.admin.entity;

import vip.dexian.admin.enums.OperateTypeEnum;
import vip.dexian.common.utils.LocalDateTimeUtils;
import vip.dexian.core.base.bean.BaseEntity;
import vip.dexian.core.base.bean.BaseLogEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Index;
import jakarta.persistence.PrePersist;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Where;

import java.io.Serial;
import java.time.LocalDateTime;

/**
 * 操作日志
 *
 * @author 挺好的 2023年06月07日 11:26
 */
@Entity
@Table (name = "br_admin_operate_log", indexes = {@Index (name = "is_valid", columnList = "isValid"),})
@DynamicUpdate
@Where (clause = BaseEntity.IS_VALID_WHERE)
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@EqualsAndHashCode (callSuper = true)
public class OperateLog extends BaseLogEntity <Long> {

    @Serial
    private static final long serialVersionUID = -774067555343859943L;

    /**
     * 功能模块
     */
    @Comment ("功能")
    @Column (nullable = false, length = 50)
    private String module;

    /**
     * 操作类型
     */
    @Comment ("操作类型")
    @Column (nullable = false, updatable = false)
    private OperateTypeEnum operateType;

    /**
     * 操作方法
     */
    @Comment ("操作方法")
    @Column (nullable = false, length = 500)
    private String method;

    /**
     * 操作员iD
     */
    @Comment ("操作员ID")
    @Column (nullable = false)
    private Long adminId;

    /**
     * 操作员
     */
    @Comment ("操作员")
    @Column (nullable = false, length = 20)
    private String username;

    /**
     * 开始时间
     */
    @Comment ("开始时间")
    @Column (nullable = false)
    private Long beginTimeMillis;

    /**
     * 结束时间
     */
    @Comment ("结束时间")
    @Column (nullable = false)
    private Long endTimeMillis;

    /**
     * 更新数据的ID
     */
    @Comment ("更新的数据ID")
    @Column
    private Long updateId;

    /**
     * 操作url
     */
    @Comment ("操作url")
    @Column
    private String operateUrl;

    /**
     * 操作描述
     */
    @Comment ("操作描述")
    @Column
    private String description;

    /**
     * 请求参数
     */
    @Comment ("请求参数")
    @Column (columnDefinition = "TEXT")
    private String request;

    /**
     * 可删除时间
     */
    @Comment ("可删除时间")
    @Column (nullable = false)
    private LocalDateTime canDeleteDate;

    /**
     * @return 是否允许删除
     */
    @Transient
    @JsonIgnore
    public Boolean getIsAllowedDelete () {
        return LocalDateTimeUtils.isAfter(LocalDateTime.now(), this.canDeleteDate);
    }


    /**
     * 保存前的操作
     */
    @PrePersist
    public void prePersist () {
        // 设置15天后才可以删除
        this.setCanDeleteDate(LocalDateTime.now().plusDays(15L));
    }
}
